﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace IMDBLib
{
    public class IMDb
    {
        private HttpWebRequest IMDbWebRequest;
        private HttpWebResponse IMDbWebResponse;
        private StreamReader IMDbWebSource;
        private string IMDbPageSource;

        public string[,] PossibleTitles;
        public IMDb_Movie Object_Movie = new IMDb_Movie();

        public bool InitializeApi(string SearchCrit, int SearchType)
        {
            bool ReturnValue = false;
            try
            {
                //Get Page
                GetWebpage(SearchCrit, SearchType);

                //Strip for Title
                switch (SearchType)
                {
                    case 1:
                        PossibleTitles = GetTitles();
                        ReturnValue = true;
                        break;
                    case 2:
                        ReturnValue = GetMovieInfo(SearchCrit);
                        break;
                }
            }
            catch (Exception)
            {
                ReturnValue = false;
            }
            return ReturnValue;
        }

        //Search Page and return Results
        private bool GetWebpage(string SearchCrit, int SearchType)
        {
            bool ReturnValue = false;

            string SearchURL = "";

            switch (SearchType)
            {
                case 1:
                    //Title
                    SearchURL = "http://www.imdb.com/find?s=--SearchType--&q=--SearchCrit--";
                    SearchURL = SearchURL.Replace("--SearchType--", "tt");
                    SearchURL = SearchURL.Replace("--SearchCrit--", SearchCrit);
                    break;
                case 2:
                    SearchURL = "http://www.imdb.com/title/--IMDbCode--/";
                    SearchURL = SearchURL.Replace("--IMDbCode--", SearchCrit);
                    break;
            }

            IMDbWebRequest = (HttpWebRequest)HttpWebRequest.Create(SearchURL);
            IMDbWebRequest.Method = "GET";
            IMDbWebResponse = (HttpWebResponse)IMDbWebRequest.GetResponse();
            IMDbWebSource = new StreamReader(IMDbWebResponse.GetResponseStream());
            IMDbPageSource = IMDbWebSource.ReadToEnd();
            IMDbWebResponse.Close();


            return ReturnValue;
        }
        //Get Possible Titles
        private string[,] GetTitles()
        {
            string[,] ReturnValue = new string[1, 2];

            string Part1 = IMDbPageSource;
            int IndexOfStart = Part1.IndexOf("<b>Titles (Partial Matches)</b>");
            Part1 = Part1.Remove(0, IndexOfStart);
            int IndexOfTableStart = (Part1.IndexOf("<table>") + 7);
            Part1 = Part1.Remove(0, IndexOfTableStart);
            int IndexOfTableEnd = (Part1.IndexOf("</table>"));
            Part1 = Part1.Remove(IndexOfTableEnd, Part1.Length - IndexOfTableEnd);
            string[] Part2 = Part1.Split(new string[] { "<tr> <td valign=\"top\">" }, StringSplitOptions.RemoveEmptyEntries);

            string Popular1 = IMDbPageSource;
            int IndexOfPopular = Popular1.IndexOf("<b>Popular Titles</b>");
            Popular1 = Popular1.Remove(0, IndexOfPopular);
            int IndexOfPopularTableStart = (Popular1.IndexOf("<table>") + 7);
            Popular1 = Popular1.Remove(0, IndexOfPopularTableStart);
            int IndexOfPopularTableEnd = (Popular1.IndexOf("</table>"));
            Popular1 = Popular1.Remove(IndexOfPopularTableEnd, Popular1.Length - IndexOfPopularTableEnd);
            string[] Popular2 = Popular1.Split(new string[] { "<tr> <td valign=\"top\">" }, StringSplitOptions.RemoveEmptyEntries);

            //Set ReturnString
            ReturnValue = new string[(Part2.Length + Popular2.Length), 2];
            int ContinueValue = 0;

            for (int i = 0; i < Popular2.Length; i++)
            {
                int StartOfEntry = Popular2[i].IndexOf("<img src=\"/images/b.gif\"");
                Popular2[i] = Popular2[i].Remove(0, StartOfEntry);
                int StartOfLink = (Popular2[i].IndexOf("<a href=\"/title/") + 16);
                Popular2[i] = Popular2[i].Remove(0, StartOfLink);

                //Set TitleCode
                ReturnValue[i, 0] = Popular2[i].Substring(0, 9);

                int StartOfTitleName = (Popular2[i].IndexOf("/';\">") + 5);
                Popular2[i] = Popular2[i].Remove(0, StartOfTitleName);
                int EndOfTitleName = Popular2[i].IndexOf("</a>");
                Popular2[i] = Popular2[i].Remove(EndOfTitleName, Popular2[i].Length - EndOfTitleName);

                ReturnValue[i, 1] = Popular2[i];
                ContinueValue = i;
            }

            ContinueValue++;

            for (int i = 0; i < Part2.Length; i++)
            {
                int StartOfEntry = Part2[i].IndexOf("<img src=\"/images/b.gif\"");
                Part2[i] = Part2[i].Remove(0, StartOfEntry);
                int StartOfLink = (Part2[i].IndexOf("<a href=\"/title/") + 16);
                Part2[i] = Part2[i].Remove(0, StartOfLink);

                //Set TitleCode
                ReturnValue[(i + ContinueValue), 0] = Part2[i].Substring(0, 9);

                int StartOfTitleName = (Part2[i].IndexOf("/';\">") + 5);
                Part2[i] = Part2[i].Remove(0, StartOfTitleName);
                int EndOfTitleName = Part2[i].IndexOf("</a>");
                Part2[i] = Part2[i].Remove(EndOfTitleName, Part2[i].Length - EndOfTitleName);

                ReturnValue[(i + ContinueValue), 1] = Part2[i];
            }

            return ReturnValue;
        }
        //Get Info by Code and set Values
        private bool GetMovieInfo(string IMDbCode)
        {
            bool ReturnValue = false;

            #region Mov_Poster
            string Part1 = IMDbPageSource;
            int IndexOfPoster = Part1.IndexOf("/" + IMDbCode + "\"");
            Part1 = Part1.Remove(0, IndexOfPoster);
            IndexOfPoster = Part1.IndexOf("http://");
            string Poster = Part1.Remove(0, IndexOfPoster);
            int EndOfPoster = (Poster.IndexOf(".jpg") + 4);

            Object_Movie.Mov_Poster = Poster.Substring(0, EndOfPoster);
            #endregion

            #region Mov_Title
            int IndexOfTitle = (Part1.IndexOf("<h1 class=\"header\">") + 20);
            string Part2 = Part1.Remove(0, IndexOfTitle);
            int EndOfTitle = Part2.IndexOf("\n");
            Object_Movie.Mov_Title = Part2.Substring(0, EndOfTitle);
            #endregion

            #region Mov_Genres
            int IndexOfGenres = (Part2.IndexOf("<h4 class=\"inline\">Genres:</h4>") + 32);
            string Part3 = Part2.Remove(0, IndexOfGenres);
            int EndOfGenres = Part3.IndexOf("\n</div>");
            Part3 = Part3.Substring(0, EndOfGenres);
            string[] Part4 = Part3.Split(new string[] { "<a href=\"/genre/" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < Part4.Length; i++)
            {
                int EndOfGenre = Part4[i].IndexOf("\">");
                Part4[i] = Part4[i].Substring(0, EndOfGenre);
            }
            Object_Movie.Mov_Genres = Part4;
            #endregion

            #region Mov_Actors
            int IndexOfActors = Part2.IndexOf("<tr class=\"odd\">");
            string Part5 = Part2.Remove(0, IndexOfActors);
            int EndOfActors = Part5.IndexOf("</table>");
            Part5 = Part5.Substring(0, EndOfActors);
            string[] Part6_1 = Part5.Split(new string[] { "<tr class=\"odd\">" }, StringSplitOptions.RemoveEmptyEntries);
            string[] Part6_2 = new string[Part6_1.Length];
            for (int i = 0; i < Part6_1.Length; i++)
            {
                try
                {
                    string[] Part6_3 = Part6_1[i].Split(new string[] { "<tr class=\"even\">" }, StringSplitOptions.RemoveEmptyEntries);
                    Part6_1[i] = Part6_3[0];
                    Part6_2[i] = Part6_3[1];
                }
                catch (Exception)
                {
                    string[] Part6_3 = new string[(Part6_1.Length - 1)];
                    for (int j = 0; j < Part6_3.Length; j++)
                    {
                        Part6_3[j] = Part6_2[j];
                    }
                    Part6_2 = new string[(Part6_1.Length - 1)];
                    Part6_3.CopyTo(Part6_2, 0);
                }
            }
            string[] Part6 = new string[(Part6_1.Length + Part6_2.Length)];
            int Index = 0;
            for (int i = 0; i < Part6.Length; i = (i + 2))
            {
                int IndexOfActor = (Part6_1[Index].IndexOf("title=\"") + 7);
                string Part6_3 = Part6_1[Index].Remove(0, IndexOfActor);
                int EndOfActor = Part6_3.IndexOf("\"");
                Part6_3 = Part6_3.Substring(0, EndOfActor);
                Part6[i] = Part6_3;
                Index++;
            }
            Index = 0;
            for (int i = 1; i < Part6.Length; i = (i + 2))
            {
                int IndexOfActor = (Part6_2[Index].IndexOf("title=\"") + 7);
                string Part6_3 = Part6_2[Index].Remove(0, IndexOfActor);
                int EndOfActor = Part6_3.IndexOf("\"");
                Part6_3 = Part6_3.Substring(0, EndOfActor);
                Part6[i] = Part6_3;
                Index++;
            }

            Object_Movie.Mov_Actors = Part6;
            #endregion

            #region Rating
            string Part7 = IMDbPageSource;
            int IndexOfRating = (Part7.IndexOf("<span class=\"rating-rating\">") + 28);
            Part7 = Part7.Remove(0, IndexOfRating);
            int EndOfRating = Part7.IndexOf("<span>");
            Part7 = Part7.Substring(0, EndOfRating);

            Object_Movie.Mov_Rating = Part7;
            #endregion

            #region Runtime
            string Part8 = IMDbPageSource;
            int IndexOfRuntime = (Part8.IndexOf("<h4 class=\"inline\">Runtime:</h4>") + 35);
            Part8 = Part8.Remove(0, IndexOfRuntime);
            int EndOfRuntime = Part8.IndexOf("\n");
            Part8 = Part8.Substring(0, EndOfRuntime);

            Object_Movie.Mov_Runtime = Part8;
            #endregion

            #region ReleaseDate
            string Part9 = IMDbPageSource;
            int IndexOfReleaseDate = (Part9.IndexOf("<h4 class=\"inline\">Release Date:</h4>") + 38);
            Part9 = Part9.Remove(0, IndexOfReleaseDate);
            int EndOfReleaseDate = Part9.IndexOf("\n");
            Part9 = Part9.Substring(0, EndOfReleaseDate);

            Object_Movie.Mov_ReleaseDate = Part9;
            #endregion

            #region MPR
            string Part10 = IMDbPageSource;
            int IndexOfMPR = (Part10.IndexOf("<h4>Motion Picture Rating (<a href=\"/mpaa\">MPAA</a>)</h4>") + 64);
            Part10 = Part10.Remove(0, IndexOfMPR);
            int EndOfMPR = Part10.IndexOf(" for ");
            Part10 = Part10.Substring(0, EndOfMPR);

            Object_Movie.Mov_MPR = Part10;
            #endregion

            #region Plot
            string Part11 = IMDbPageSource;
            int IndexOfPlot = (Part11.IndexOf("<h2>Storyline</h2>") + 23);
            Part11 = Part11.Remove(0, IndexOfPlot);
            int EndOfPlot = Part11.IndexOf("\n\n<em class=");
            Part11 = Part11.Substring(0, EndOfPlot);

            Object_Movie.Mov_Plot = Part11;
            #endregion

            #region Stars
            string Part12 = IMDbPageSource;
            int IndexOfStars = (Part12.IndexOf("<h4 class=\"inline\">Stars:</h4>\n") + 31);
            Part12 = Part12.Remove(0, IndexOfStars);
            int EndOfStars = Part12.IndexOf("\n</div>");
            Part12 = Part12.Substring(0, EndOfStars);

            string[] Part13 = Part12.Split(new string[] { "<a    onclick=" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < Part13.Length; i++)
            {
                string Star = Part13[i];
                int IndexOfStar = (Star.IndexOf("/\"       >") + 10);
                Star = Star.Remove(0, IndexOfStar);
                int EndOfStar = Star.IndexOf("</a>");
                Part13[i] = Star.Substring(0, EndOfStar);
            }

            Object_Movie.Mov_Stars = Part13;
            #endregion

            #region Writers
            string Part14 = IMDbPageSource;
            int IndexOfWriters = (Part14.IndexOf("<h4 class=\"inline\">\n  Writers:\n  </h4>\n\n") + 40);
            Part14 = Part14.Remove(0, IndexOfWriters);
            int EndOfWriters = Part14.IndexOf("\n</div>");
            Part14 = Part14.Substring(0, EndOfWriters);

            string[] Part15 = Part14.Split(new string[] { "<a" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < Part15.Length; i++)
            {
                string Writer = Part15[i];
                int IndexOfWriter = (Writer.IndexOf("\">") + 2);
                Writer = Writer.Remove(0, IndexOfWriter);
                int EndOfWriter = Writer.IndexOf("</a>");
                Writer = Writer.Substring(0, EndOfWriter);
                if (Writer.StartsWith("\n"))
                {
                    Writer = Writer.Remove(0, 1);
                }
                Part15[i] = Writer;
            }

            Object_Movie.Mov_Writers = Part15;
            #endregion

            #region Directors
            string Part16 = IMDbPageSource;
            int IndexOfDirectors = (Part16.IndexOf("<h4 class=\"inline\">\n    Director:\n  </h4>\n") + 42);
            if (IndexOfDirectors <= 0)
            {
                IndexOfDirectors = (Part16.IndexOf("<h4 class=\"inline\">\n    Directors:\n  </h4>\n") + 43);
            }
            Part16 = Part16.Remove(0, IndexOfDirectors);
            int EndOfDirectors = Part16.IndexOf("</div>");
            Part16 = Part16.Substring(0, EndOfDirectors);

            string[] Part17 = Part16.Split(new string[] { "<a" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < Part17.Length; i++)
            {
                string Director = Part17[i];
                int IndexOfDirector = (Director.IndexOf("\">") + 2);
                Director = Director.Remove(0, IndexOfDirector);
                int EndOfDirector = Director.IndexOf("</a>");
                Director = Director.Substring(0, EndOfDirector);
                if (Director.StartsWith("\n"))
                {
                    Director = Director.Remove(0, 1);
                }
                Part17[i] = Director;
            }

            Object_Movie.Mov_Directors = Part17;
            #endregion

            return ReturnValue;
        }
    }
}
